package com.compass.mobile.ui.activity


import UserInfoManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.compass.mobile.R
import com.compass.mobile.databinding.ActivityDoctorHomePageBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.constant.BACKSPACE_ACTION
import com.compass.mobile.framework.constant.CANCEL_ACTION
import com.compass.mobile.framework.constant.CLINICAL_RECEPTION
import com.compass.mobile.framework.constant.END_ACTION
import com.compass.mobile.framework.constant.INQUIRY_ACTION
import com.compass.mobile.framework.constant.KEY_DEPARTMENT_ID
import com.compass.mobile.framework.constant.KEY_DOCTOR_ID
import com.compass.mobile.framework.constant.KEY_TYPE
import com.compass.mobile.framework.constant.PATIENT_SWITCH
import com.compass.mobile.framework.constant.PRESCRIPTION_ACTION
import com.compass.mobile.framework.constant.REFERRAL_ACTION
import com.compass.mobile.framework.utils.LiveDataBus
import com.compass.mobile.models.Attach
import com.compass.mobile.ui.fragment.DoctorInfoFragment
import com.compass.mobile.ui.fragment.InquiryFragment
import com.compass.mobile.ui.viewModel.DoctorViewModel


/**
 * Description：医生主页Fragment
 * Date:2024/6/3 22:30
 */
class DoctorHomePageActivity : BaseVBActivity<ActivityDoctorHomePageBinding>() {


    companion object {
        fun start(context: Context, doctorId: String, departmentId: String) {
            val intent = Intent(context, DoctorHomePageActivity::class.java)
            intent.putExtra(KEY_DOCTOR_ID, doctorId)
            intent.putExtra(KEY_DEPARTMENT_ID, departmentId)
            context.startActivity(intent)
        }
    }
    private val doctorViewModel: DoctorViewModel by viewModels()
    private lateinit var doctorInfoFragment: DoctorInfoFragment
    private lateinit var inquiryFragment: InquiryFragment
    private var doctorId: String? = null
    private var departmentId: String? = null
    private var type: String? = null
    override fun initView(savedInstanceState: Bundle?) {
        doctorId = intent.getStringExtra(KEY_DOCTOR_ID)
        departmentId = intent.getStringExtra(KEY_DEPARTMENT_ID)

        doctorInfoFragment = DoctorInfoFragment()
        val bundle = Bundle()
        bundle.putString(KEY_TYPE, "Info")
        bundle.putString(KEY_DOCTOR_ID, doctorId)
        bundle.putString(KEY_DEPARTMENT_ID, departmentId)
        doctorInfoFragment.arguments = bundle
        displayDoctorFragment(doctorInfoFragment)

        inquiryFragment = InquiryFragment()
        inquiryFragment.arguments = bundle
        displayInquiryFragment(inquiryFragment)

        // 切换就诊人需要刷新
        LiveDataBus.get().with(PATIENT_SWITCH, String::class.java) .observe(this) {
            mBinding.topView.setDefaultPatientInfo()
        }
    }

    override fun initData() {
        showLoading()
        getDoctorInfo()

        doctorViewModel.doctorHomePageLiveData.observe(this){
            dismissLoading()
        }

        LiveDataBus.get().with(END_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()
            }
        LiveDataBus.get().with(INQUIRY_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}
        LiveDataBus.get().with(PRESCRIPTION_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}
       LiveDataBus.get().with(CANCEL_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}

        LiveDataBus.get().with(BACKSPACE_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}
        LiveDataBus.get().with(CLINICAL_RECEPTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}
        LiveDataBus.get().with(REFERRAL_ACTION, Attach::class.java)
            .observe(this) {
                getDoctorInfo()}


        // 切换就诊人需要刷新
        LiveDataBus.get().with(PATIENT_SWITCH, String::class.java)
            .observe(this) {
                getDoctorInfo()
            }

    }

    private fun getDoctorInfo(){
        doctorViewModel.getDoctorHomePage(
            doctorId,
            UserInfoManager.getPatientInfo()?.id,
            departmentId
        )

    }


    // 根据需要显示或替换Fragment
    private fun displayDoctorFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentDoctor, fragment)
        fragmentTransaction.commit()
    }

    // 根据需要显示或替换Fragment
    private fun displayInquiryFragment(fragment: Fragment) {
        val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentInquiry, fragment)
        fragmentTransaction.commit()
    }

}